package cn.janus.config;

import cn.janus.component.JwtServerSecurityContextRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;


@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {

    @Autowired
    private JwtServerSecurityContextRepository jwtServerSecurityContextRepository;

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http
                .httpBasic().disable() // 禁用HTTP Basic认证
                .formLogin().disable() // 禁用表单登录
                .csrf().disable() // 禁用CSRF保护
                .securityContextRepository(jwtServerSecurityContextRepository)
                .authorizeExchange()
                .pathMatchers("/user/login").permitAll() // 允许所有用户访问/public/路径下的资源
                .pathMatchers("/user/**").hasRole("ADMIN")
                .pathMatchers("/tally/**").hasRole("ADMIN")
                .anyExchange().authenticated(); // 其他所有路径都需要认证

        return http.build();
    }
}
